package com.oniontech.stamptour.fragment.app;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.oniontech.stamptour.R;
import com.oniontech.stamptour.common.ui.CustomDialog;
import com.oniontech.stamptour.fragment.DefaultFragment;
import com.oniontech.stamptour.module.encryption.CryptString;

/**
 * Created by limsy on 14. 1. 27.
 */
public class StampMainToMailFragment extends DefaultFragment {


    EditText edit_name, edit_phone, edit_email;
    Button btn_certi, btn_email;
    TextView text_certi;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.stamptomail, null);
        view.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        Button btn_what = (Button)view.findViewById(R.id.btn_what);
        btn_what.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CustomDialog makedialog = new CustomDialog(getActivity());
                makedialog.setDialogTitle(getResources().getString(
                        R.string.app_what));
                makedialog.setDialogMsg(getResources().getString(
                        R.string.msg_what));
                makedialog.setBtnText("확인", "확인");
                makedialog.setCustomDialog(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        switch (v.getId()) {
                            case R.id.btn_dial_confirm:
                                makedialog.Dismiss();
                                break;

                            case R.id.btn_dial_cancel:
                                // 설정
                                makedialog.Dismiss();
                                break;
                        }
                    }
                }, false);

                makedialog.Show();
            }
        });

        edit_name = (EditText)view.findViewById(R.id.edit_name);
        edit_phone = (EditText)view.findViewById(R.id.edit_phone);
        edit_email = (EditText)view.findViewById(R.id.edit_email);

        btn_certi = (Button)view.findViewById(R.id.btn_certi);
        btn_certi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkFeild()) {

                    getCertiKey();
                }
            }
        });

        text_certi = (TextView)view.findViewById(R.id.text_certi);
        text_certi.setVisibility(View.GONE);

        btn_email = (Button)view.findViewById(R.id.btn_email);

        return view;
    }

    public Boolean checkFeild() {

        final CustomDialog makedialog = new CustomDialog(getActivity());
        makedialog.setDialogTitle(getResources().getString(
                R.string.app_info));
        makedialog.setBtnText("확인", "확인");
        makedialog.setCustomDialog(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                    case R.id.btn_dial_confirm:
                        makedialog.Dismiss();
                        break;

                    case R.id.btn_dial_cancel:
                        // 설정
                        makedialog.Dismiss();
                        break;
                }
            }
        }, false);


        if(edit_name.getText().toString().equals("")) {
            makedialog.setDialogMsg(String.format(getResources().getString(R.string.msg_error), "성 명"));
            makedialog.Show();
            return false;
        }else if(edit_phone.getText().toString().equals("")) {
            makedialog.setDialogMsg(String.format(getResources().getString(R.string.msg_error), "전화번호"));
            makedialog.Show();
            return false;
        }else if(edit_email.getText().toString().equals("")) {
            makedialog.setDialogMsg(String.format(getResources().getString(R.string.msg_error), "e-mail"));
            makedialog.Show();
            return false;
        }
        return true;
    }

    public void getCertiKey() {

        final String des_64 = CryptString.getEncryptString(getSend_Code());
        System.out.println(des_64);

        btn_certi.setVisibility(View.GONE);
        text_certi.setVisibility(View.VISIBLE);
        text_certi.setText(des_64);

        btn_email.setBackgroundResource(R.drawable.btn_email_n_selector);

        btn_email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEmail(des_64);
            }
        });

    }

    public String getSend_Code() {
        m_DBMgr.openPointDB(DB_TYPE_ADDRESS, true);


        StringBuffer sb = new StringBuffer();
        StringBuffer dec_sb = new StringBuffer();

        sb.append("SELECT send_code FROM contents where visit=1 \n");

        Cursor cursor = m_DBMgr.DBQuery(sb.toString());

        if (cursor == null || !cursor.moveToFirst())
        {
            m_DBMgr.closeDB();
            return null;
        }

        int cnt = cursor.getCount();

        if(cnt == 0) {
            return null;
        }

        for (int i = 0; i < cnt; i++) {
            if(i == cnt-1) {
                dec_sb.append(cursor.getString(cursor.getColumnIndex("send_code")));
            }else {
                dec_sb.append(cursor.getString(cursor.getColumnIndex("send_code")) + "^");
            }

            cursor.moveToNext();
        }

        cursor.close();
        m_DBMgr.closeDB();

        return dec_sb.toString() + ";" + edit_name.getText().toString() + ";" + edit_phone.getText().toString() + ";" + edit_email.getText().toString();
    }

    public void sendEmail(String text) {
        Intent email = new Intent(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL, new String[]{"heritage@seoul.go.kr"});
        email.putExtra(Intent.EXTRA_SUBJECT, "2015년도 서울시 문화 지킴이 신청");
        email.putExtra(Intent.EXTRA_TEXT, getMailMSG(text));
        email.setType("message/rfc822");
        startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }

    public String getMailMSG(String text) {
        StringBuffer sb = new StringBuffer();

        sb.append("2015년도 서울시 문화재 스탬프 투어 답사를 완료하여, 문화 지킴이를 신청합니다.\n\n");
        sb.append("<기본정보>\n");
        sb.append("*성   명 : " + edit_name.getText().toString()+"\n");
        sb.append("*전화번호 : " + edit_phone.getText().toString()+"\n");
        sb.append("* e-mail : " + edit_email.getText().toString()+"\n\n");
        sb.append("<인증키>\n");
        sb.append(text);


        return  sb.toString();
    }
}
